Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • When my loop*reaches the 101st iteration, it fails

    Hi everyone,

    I'm encountering an issue with a loop in Stata where the loop stops at the 100th iteration and throws an error message. It seems like there's a limitation on the number of timers that can be used, which is set to 100 by default. When my loop reaches the 101st iteration, it fails.

    Is there a way to increase this limit from 100 to a higher value, like 300, so that my loop can run without interruption?

    Thanks in advance for your help!

    Code:
    local listx h_gender hh_age hh_maritst edu1 edu2 edu3 edu4 hh_size child0_5 urban land_size female_hh 
    *local index spi zscore delta
    local depv d_migr d_migr_internal d_migr_abroad n_migr n_migr_abroad n_migr_internal sh_migr sh_migr_abroad sh_migr_internal totremit d_remit d_tremit
    local j 1
    local i 1
    foreach y in `depv' {
    foreach ind in `index' {
    forvalue d=1(1)18{
    
    timer on `j'
        local opt= cond(`i'==1, "replace", "append")
        local listx "h_gender hh_age hh_maritst edu1 edu2 edu3 edu4 hh_size child0_5 urban land_size female_hh"
        quietly xtreg `y' did`d'_`ind' treated`d'_`ind' `listx' i.year,  fe vce (cluster coato)                       
    outreg2 using "C:\Users\User\Desktop\corrtables\DiD.xls"
        local i=`i'+1
        local opt= cond(`i'==1, "replace", "append")
    
    timer off `j'
    timer list `j'
    display as text "Estimation for `ind'" "Elapsed time in minutes: " as result %3.2f `=(`r(t`j')')/60'
    timer clear `j'
    local j=`j'+1
    }
    }
    }

  • #2
    Indeed. I didn't know there was such a limit, but it's documented in the manual (a.k.a. the PDF documentation), either alongside your Stata or at www.stata.com/manuals/ptimer.pdf

    Comment


    • #3
      You can clear the timer after every 100 iterations. Consider this:

      Code:
      sysuse auto, clear
      local j 0
      forval i=1/120{
          local ++j
          local timerclear= cond(`j'==1, "timer clear", "")
          `timerclear'
          timer on `j'
          display "THIS IS ITERATION `i'; TIMER= `j'"
          regress mpg weight
          timer off `j'
          timer list `j'
          local j= cond(!mod(`i',100), 0, `j')
      }
      Res.:

      Code:
      THIS IS ITERATION 99; TIMER= 99
      
            Source |       SS           df       MS      Number of obs   =        74
      -------------+----------------------------------   F(1, 72)        =    134.62
             Model |   1591.9902         1   1591.9902   Prob > F        =    0.0000
          Residual |  851.469256        72  11.8259619   R-squared       =    0.6515
      -------------+----------------------------------   Adj R-squared   =    0.6467
             Total |  2443.45946        73  33.4720474   Root MSE        =    3.4389
      
      ------------------------------------------------------------------------------
               mpg | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
      -------------+----------------------------------------------------------------
            weight |  -.0060087   .0005179   -11.60   0.000    -.0070411   -.0049763
             _cons |   39.44028   1.614003    24.44   0.000     36.22283    42.65774
      ------------------------------------------------------------------------------
        99:      0.01 /        1 =       0.0050
      THIS IS ITERATION 100; TIMER= 100
      
            Source |       SS           df       MS      Number of obs   =        74
      -------------+----------------------------------   F(1, 72)        =    134.62
             Model |   1591.9902         1   1591.9902   Prob > F        =    0.0000
          Residual |  851.469256        72  11.8259619   R-squared       =    0.6515
      -------------+----------------------------------   Adj R-squared   =    0.6467
             Total |  2443.45946        73  33.4720474   Root MSE        =    3.4389
      
      ------------------------------------------------------------------------------
               mpg | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
      -------------+----------------------------------------------------------------
            weight |  -.0060087   .0005179   -11.60   0.000    -.0070411   -.0049763
             _cons |   39.44028   1.614003    24.44   0.000     36.22283    42.65774
      ------------------------------------------------------------------------------
       100:      0.00 /        1 =       0.0040
      THIS IS ITERATION 101; TIMER= 1
      
            Source |       SS           df       MS      Number of obs   =        74
      -------------+----------------------------------   F(1, 72)        =    134.62
             Model |   1591.9902         1   1591.9902   Prob > F        =    0.0000
          Residual |  851.469256        72  11.8259619   R-squared       =    0.6515
      -------------+----------------------------------   Adj R-squared   =    0.6467
             Total |  2443.45946        73  33.4720474   Root MSE        =    3.4389
      
      ------------------------------------------------------------------------------
               mpg | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
      -------------+----------------------------------------------------------------
            weight |  -.0060087   .0005179   -11.60   0.000    -.0070411   -.0049763
             _cons |   39.44028   1.614003    24.44   0.000     36.22283    42.65774
      ------------------------------------------------------------------------------
         1:      0.01 /        1 =       0.0050
      Last edited by Andrew Musau; 03 Sep 2024, 03:12.

      Comment


      • #4
        Hı Nıck, Hi Andrew, A big thank you for your help! It turns out the timer was causing the issue. Once I removed it, the loop ran perfectly.

        Comment

        Working...
        X